//=============================================================================
/**
 *
 *	@file		egg_large_yesno.c
 *	@brief		LCD_SUB全面を使用するBMP選択ウィンドウモジュール
 *	@author		hosaka genya
 *	@data		2009.03.25
 *	@note		LCD_SUBを独占。BANK割り当てが32kあれば使用できます。
 *
 */
//=============================================================================
#include "common.h"
#include "system/arc_tool.h"
#include "system/arc_util.h"
#include "system/window.h"

#include "system/bmp_menu.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/font_arc.h"
#include "system/msgdata_util.h"
#include "system/wordset.h"

#include "system/clact_util.h"
#include "system/clact_tool.h"

#include "gflib/touchpanel.h"

#include "msgdata/msg.naix"
#include "application/subselect.naix"

#include "system/snd_tool.h"

#include "egg_large_yesno.h"

//=============================================================================
/**
 *								定数定義
 */
//=============================================================================
// はい・いいえ用BMPWIN定義
#define YESNO_BMP_X		( 4 )
#define YESNO_BMP_Y1	(  8 )
#define YESNO_BMP_Y2	( 14 )
#define YESNO_BMP_W		(  24 )
#define YESNO_BMP_H		(  2 )
#define YESNO_BMP_OFFSET	( 80 )

static const s16 yesno_cursor_pos_tbl[][2]={
	{ 128, 72 },
	{ 128, 120 },
};

static const RECT_HIT_TBL yesno_touch_tbl[] = {
	{ 50,  92, 3, 251, },
	{ 99, 140, 3, 251, },
	{TP_HIT_END,0,0,0},		// 終了データ
};

//=============================================================================
/**
 *								構造体定義
 */
//=============================================================================

//--------------------------------------------------------------
///	メインワーク
//==============================================================
typedef struct LARGE_YESNO_WORK_tag {
	// [IN]
	HEAPID HeapID;
	CATS_SYS_PTR csp;
	CATS_RES_PTR crp;
	GF_BGL_INI* bgl;

	// [PRIVATE]
	CATS_ACT_PTR	capCursor;
	WORDSET*		wordset;

	u8				select_seq;
	u8				select_ret;
	GF_BGL_BMPWIN	YesNoBmpWin[2];
} LARGE_YESNO_WORK;


//=============================================================================
/**
 *							プロトタイプ宣言
 */
//=============================================================================

static void Bmpwin_Add( LARGE_YESNO_PTR ptr );
static void Bmpwin_Del( LARGE_YESNO_PTR ptr );
static void Bmpwin_Msgdraw( LARGE_YESNO_PTR ptr, GF_BGL_BMPWIN *win, int msgno );
static void Obj_Create( LARGE_YESNO_PTR ptr, int resid_char, int resid_pal, int resid_cell, int resid_anm );
static void Obj_Del( LARGE_YESNO_PTR ptr );
static void Obj_SetCursor( LARGE_YESNO_PTR ptr );
static BOOL Seq_Input(LARGE_YESNO_PTR ptr);
static BOOL Seq_AnimeWait( LARGE_YESNO_PTR ptr );


//=============================================================================
/**
 *								外部公開関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	bgl
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_SetSubBGFrame(GF_BGL_INI* bgl)
{
	{
		GF_BGL_BGCNT_HEADER TextBgCntDat[] = {
			{	///< BG0
				0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x7000, GX_BG_CHARBASE_0x04000, GX_BG_EXTPLTT_01,
				2, 0, 0, FALSE
			},
			{	///< BG1
				0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x6800, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
				1, 0, 0, FALSE
			},
			{	///< BG2
				0, 0, 0x800, 0, GF_BGL_SCRSIZ_256x256, GX_BG_COLORMODE_16,
				GX_BG_SCRBASE_0x6000, GX_BG_CHARBASE_0x00000, GX_BG_EXTPLTT_01,
				0, 0, 0, FALSE
			},
		};
		// 背景
		GF_BGL_BGControlSet(bgl, GF_BGL_FRAME0_S, &TextBgCntDat[0], GF_BGL_MODE_TEXT);
		GF_BGL_ScrClear(bgl, GF_BGL_FRAME0_S);
		// 選択肢 背景
		GF_BGL_BGControlSet(bgl, GF_BGL_FRAME1_S, &TextBgCntDat[1], GF_BGL_MODE_TEXT);
		GF_BGL_ScrClear(bgl, GF_BGL_FRAME1_S);
		// 選択肢
		GF_BGL_BGControlSet(bgl, GF_BGL_FRAME2_S, &TextBgCntDat[2], GF_BGL_MODE_TEXT);
		GF_BGL_ScrClear(bgl, GF_BGL_FRAME2_S);
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	GF_BGL_INI* bgl
 *	@param	csp
 *	@param	crp
 *	@param	HeapID
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
LARGE_YESNO_PTR LargeYesno_Create( GF_BGL_INI* bgl, CATS_SYS_PTR csp, CATS_RES_PTR crp, HEAPID HeapID )
{
	LARGE_YESNO_PTR ptr;

	GF_ASSERT( bgl );
	GF_ASSERT( csp );
	GF_ASSERT( crp );

	ptr = sys_AllocMemory( HeapID, sizeof(LARGE_YESNO_WORK) );
	MI_CpuClear8( ptr, sizeof(LARGE_YESNO_WORK) );

	ptr->HeapID = HeapID;
	ptr->bgl = bgl;
	ptr->csp = csp;
	ptr->crp = crp;
	ptr->wordset = WORDSET_Create( HeapID );

	// タッチフォントをロード
	FontProc_LoadFont( FONT_TOUCH, ptr->HeapID );

	Bmpwin_Add( ptr );

	return ptr;
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_Delete( LARGE_YESNO_PTR ptr )
{
	GF_ASSERT( ptr );

	Bmpwin_Del( ptr );
	Obj_Del( ptr );

	FontProc_UnloadFont( FONT_TOUCH );
	WORDSET_Delete( ptr->wordset );

	GF_BGL_BGControlExit(ptr->bgl, GF_BGL_FRAME0_S);
	GF_BGL_BGControlExit(ptr->bgl, GF_BGL_FRAME1_S);
	GF_BGL_BGControlExit(ptr->bgl, GF_BGL_FRAME2_S);

	sys_FreeMemoryEz( ptr );
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *	@param	pfd
 *	@param	resid_char
 *	@param	resid_pal
 *	@param	resid_cell
 *	@param	resid_anm
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_LoadRes( LARGE_YESNO_PTR ptr, PALETTE_FADE_PTR pfd, int resid_char, int resid_pal, int resid_cell, int resid_anm )
{
	ARCHANDLE* arcSubSelect;
	CATS_SYS_PTR csp = ptr->csp;
	CATS_RES_PTR crp = ptr->crp;

	GF_ASSERT( ptr );
	GF_ASSERT( csp );
	GF_ASSERT( crp );

	arcSubSelect = ArchiveDataHandleOpen( ARC_SUBSELECT_GRA, ptr->HeapID );

	CATS_LoadResourcePlttArcH( csp, crp, arcSubSelect, NARC_subselect_bmpmenu_obj_nclr, 0, 1,  NNS_G2D_VRAM_TYPE_2DSUB, resid_pal );
	CATS_LoadResourceCharArcH( csp, crp, arcSubSelect, NARC_subselect_bmpmenu_obj_ncgr, 0, NNS_G2D_VRAM_TYPE_2DSUB, resid_char );
	CATS_LoadResourceCellArcH( csp, crp, arcSubSelect, NARC_subselect_bmpmenu_obj_ncer, 0, resid_cell );
	CATS_LoadResourceCellAnmArcH( csp, crp, arcSubSelect, NARC_subselect_bmpmenu_obj_nanr, 0, resid_anm );

	// CAP ADD
	Obj_Create( ptr, resid_char, resid_pal, resid_cell, resid_anm );

	//サブ画面背景グラフィックデータロード
	{
		int arc_index = ARC_SUBSELECT_GRA;
		int pal_index1 = NARC_subselect_bmpmenu_nclr;
//		int pal_index2 = NARC_subselect_bmpmenu_back2_nclr;
		int chr_index = NARC_subselect_bmpmenu_bg_ncgr;
		int scr_index = NARC_subselect_bmpmenu_back_nscr;
		int frame	  = GF_BGL_FRAME0_S;
		int heap_id	  = HEAPID_EGG_DEMO;

		// パレット転送
		PaletteWorkSet_Arc(pfd, arc_index, pal_index1, heap_id, FADE_SUB_BG, 0x20*5, 0);
//		PaletteWorkSet_Arc(pfd, arc_index, pal_index2, heap_id, FADE_SUB_BG, 0x20*1, 0x10*PALOFS_SUB_WIN);
		PaletteWorkSet_Arc(pfd, ARC_FONT, NARC_font_touch_ncrl, heap_id, FADE_SUB_BG, 0x20*1, 0x10*PALOFS_SUB_TEXT);

		// 背景
		ArcUtil_HDL_BgCharSet( arcSubSelect, chr_index, ptr->bgl, frame, 0, 0, 0, heap_id);
		ArcUtil_HDL_ScrnSet( arcSubSelect, scr_index, ptr->bgl, frame, 0, 0, 0, heap_id);

		chr_index = NARC_subselect_bmpmenu_bg_ncgr;
		scr_index = NARC_subselect_bmpmenu_yn_nscr;
		frame	  = GF_BGL_FRAME0_S;

		// 選択肢
		ArcUtil_HDL_BgCharSet( arcSubSelect, NARC_subselect_bmpmenu_bg_ncgr, ptr->bgl, GF_BGL_FRAME1_S, 0, 0, 0, heap_id);
		ArcUtil_HDL_ScrnSet( arcSubSelect, NARC_subselect_bmpmenu_yn_nscr, ptr->bgl, GF_BGL_FRAME1_S, 0, 0, 0, heap_id);

		// BG選択肢、テキストは非表示にしておく
		GF_BGL_VisibleSet( GF_BGL_FRAME1_S, VISIBLE_OFF );
		GF_BGL_VisibleSet( GF_BGL_FRAME2_S, VISIBLE_OFF );

	}

	//ＯＢＪの表示
	GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ, VISIBLE_ON);

	ArchiveDataHandleClose( arcSubSelect );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	選択肢を初期化します。
 *
 *	@param	ptr
 *	@param	msg1:YESに
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void LargeYesno_Req(LARGE_YESNO_PTR ptr, int msg1, int msg2 )
{

	// 描画
	Bmpwin_Msgdraw( ptr, &ptr->YesNoBmpWin[0], msg1 );
	Bmpwin_Msgdraw( ptr, &ptr->YesNoBmpWin[1], msg2 );

	ptr->select_seq = 0;
	ptr->select_ret = LARGE_YESNO_YES;
	Obj_SetCursor( ptr );

	// 選択肢BG表示
	GF_BGL_VisibleSet( GF_BGL_FRAME1_S, VISIBLE_ON );
	GF_BGL_VisibleSet( GF_BGL_FRAME2_S, VISIBLE_ON );

	//ＯＢＪの表示
	GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON) ;
	CATS_ObjectEnableCap( ptr->capCursor, CATS_ENABLE_TRUE );
}


//-----------------------------------------------------------------------------
/**
 *	@brief	選択肢 入力待ち
 *
 *	@param	ptr
 *
 *	@retval	LARGE_YESNO_RES:入力結果を返す
 */
//-----------------------------------------------------------------------------
LARGE_YESNO_RES LargeYesno_ChcekInputProc(LARGE_YESNO_PTR ptr)
{
	switch(ptr->select_seq)
	{
		case 0:
			if( Seq_Input(ptr) ) {
				ptr->select_seq++;
			}
			break;
		case 1:
			// アニメ終了待ち
			if( Seq_AnimeWait(ptr) ){
				GF_ASSERT( ptr->select_ret != LARGE_YESNO_NONE );
				GF_BGL_VisibleSet( GF_BGL_FRAME1_S, VISIBLE_OFF );
				GF_BGL_VisibleSet( GF_BGL_FRAME2_S, VISIBLE_OFF );
				CATS_ObjectEnableCap( ptr->capCursor, CATS_ENABLE_FALSE );
				GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_OFF );
				return ptr->select_ret;
			}
			break;

		default : GF_ASSERT(0);
	}

	return LARGE_YESNO_NONE;
}




//=============================================================================
/**
 *								static関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Bmpwin_Add( LARGE_YESNO_PTR ptr )
{
	GF_BGL_BmpWinAdd( ptr->bgl, &ptr->YesNoBmpWin[0], GF_BGL_FRAME2_S,
					  YESNO_BMP_X, YESNO_BMP_Y1, YESNO_BMP_W, YESNO_BMP_H,
					  PALOFS_SUB_TEXT, YESNO_BMP_OFFSET	);

	GF_BGL_BmpWinAdd( ptr->bgl, &ptr->YesNoBmpWin[1], GF_BGL_FRAME2_S,
					  YESNO_BMP_X, YESNO_BMP_Y2, YESNO_BMP_W, YESNO_BMP_H,
					  PALOFS_SUB_TEXT, YESNO_BMP_OFFSET+YESNO_BMP_W*YESNO_BMP_H );
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Bmpwin_Del( LARGE_YESNO_PTR ptr )
{
	int i;

	GF_ASSERT( ptr );

	for( i=0; i<2; i++ )
	{
		GF_BGL_BmpWinDel( &ptr->YesNoBmpWin[i] );
	}
}

//------------------------------------------------------------------
/**
 * @brief   BMPWINにメッセージ描画
 *
 * @param   win
 * @param   msgno
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void Bmpwin_Msgdraw( LARGE_YESNO_PTR ptr, GF_BGL_BMPWIN *win, int msgno )
{
	STRBUF *strbuf;
	MSGDATA_MANAGER* msg_man;

	msg_man = MSGMAN_Create( MSGMAN_TYPE_NORMAL, ARC_MSG, NARC_msg_egg_demo_dat, ptr->HeapID );
	strbuf = MSGDAT_UTIL_AllocExpandString( ptr->wordset, msg_man, msgno, ptr->HeapID );

	GF_BGL_BmpWinDataFill( win,  0x0000 );
	GF_STR_PrintColor( win, FONT_TOUCH, strbuf, 0, 0, MSG_ALLPUT, GF_PRINTCOLOR_MAKE( 1,2,0 ),NULL);
	GF_BGL_BmpWinOn( win );

	MSGMAN_Delete( msg_man );
	STRBUF_Delete( strbuf );
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *	@param	resid_char
 *	@param	resid_pal
 *	@param	resid_cell
 *	@param	resid_anm
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Obj_Create( LARGE_YESNO_PTR ptr, int resid_char, int resid_pal, int resid_cell, int resid_anm )
{
	TCATS_OBJECT_ADD_PARAM_S prm = {
		0, 0, 0,
		0, 0, TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DSUB,
		{ 0, 0, 0, 0, 0, 0 }, 0, FALSE,
	};

	CATS_ACT_PTR cap;
	CATS_SYS_PTR csp = ptr->csp;
	CATS_RES_PTR crp = ptr->crp;

	GF_ASSERT( ptr );
	GF_ASSERT( csp );
	GF_ASSERT( crp );

	prm.x = yesno_cursor_pos_tbl[0][0];
	prm.y = yesno_cursor_pos_tbl[0][1];
	prm.anm = 1;
	prm.id[0] = resid_char;
	prm.id[1] = resid_pal;
	prm.id[2] = resid_cell;
	prm.id[3] = resid_anm;

	cap = CATS_ObjectAdd_S( csp, crp, &prm );
	CATS_ObjectAutoAnimeSetCap( cap, TRUE );
	CATS_ObjectEnableCap( cap, CATS_ENABLE_FALSE );

	ptr->capCursor = cap;
}

//-----------------------------------------------------------------------------
/**
 *	@brief
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Obj_Del( LARGE_YESNO_PTR ptr )
{
	CATS_ActorPointerDelete_S( ptr->capCursor );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	カーソルCAP 座標セット
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Obj_SetCursor( LARGE_YESNO_PTR ptr )
{
	int idx;

	GF_ASSERT( ptr->capCursor );

	switch( ptr->select_ret )
	{
		case LARGE_YESNO_YES : idx = 0; break;
		case LARGE_YESNO_NO  : idx = 1; break;
		default : GF_ASSERT(0);
	}

	CATS_ObjectPosSetCap( ptr->capCursor, yesno_cursor_pos_tbl[idx][0], yesno_cursor_pos_tbl[idx][1] );
}

//-----------------------------------------------------------------------------
/**
 *	@brief	シーケンス：入力
 *
 *	@param	ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL Seq_Input(LARGE_YESNO_PTR ptr)
{
	BOOL b_ret = FALSE;
	u32 tp_ret = GF_TP_RectHitTrg( yesno_touch_tbl );

	if( tp_ret != RECT_HIT_NONE ) {
		Snd_SePlay( SE_DECIDE );
		switch( tp_ret ) {
			case 0 : ptr->select_ret = LARGE_YESNO_YES; b_ret = TRUE; break;
			case 1 : ptr->select_ret = LARGE_YESNO_NO; b_ret = TRUE; break;
			default : GF_ASSERT(0);
		}
	}
	// KEY操作
	else
	{
		if(sys.trg&PAD_KEY_UP){
			if( ptr->select_ret != LARGE_YESNO_YES )
			{
				ptr->select_ret = LARGE_YESNO_YES;
				Obj_SetCursor( ptr );
				Snd_SePlay( SE_DECIDE );
			}
		}else if(sys.trg&PAD_KEY_DOWN){
			if( ptr->select_ret != LARGE_YESNO_NO )
			{
				ptr->select_ret = LARGE_YESNO_NO;
				Obj_SetCursor( ptr );
				Snd_SePlay( SE_DECIDE );
			}
		}else if(sys.trg&PAD_BUTTON_DECIDE){
			// 不具合対策
			if( ptr->select_ret == LARGE_YESNO_NONE ) {
				GF_ASSERT(0);
				ptr->select_ret = LARGE_YESNO_NO;
			}
			b_ret = TRUE;
		}else if(sys.trg&PAD_BUTTON_CANCEL){
			ptr->select_ret = LARGE_YESNO_NO;
			b_ret = TRUE;
		}
	}

	if( b_ret ) {
		Obj_SetCursor( ptr );
		CATS_ObjectAnimeSeqSetCap( ptr->capCursor, 3 );
		Snd_SePlay( SE_DECIDE );
	}

	return b_ret;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	シーケンス：アニメ終了待ち
 *
 *	@param	LARGE_YESNO_PTR ptr
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static BOOL Seq_AnimeWait( LARGE_YESNO_PTR ptr )
{
	GF_ASSERT( CATS_ObjectAnimeSeqGetCap(ptr->capCursor) == 3 );

	if( CATS_ObjectAnimeActiveCheckCap(ptr->capCursor) == FALSE ) {
		return TRUE;
	}

	return FALSE;
}

